﻿// -----------------------------------------------------------------------
// <copyright file="Utility.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace HashMaster.Monitor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using  HashMaster.Lib;
    using System.Net;
    using System.Web;
    using System.Runtime.Serialization;
    using System.Xml.Serialization;
    

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class Utility
    {
        private static WebResponse ResponseBuilder(string requestUrl,string method) 
        {
            WebRequest request = WebRequest.Create(requestUrl);
            request.Method = method;
            if (method=="PUT")
            {
                request.ContentLength = 0;
            }
            WebResponse response = request.GetResponse();
            return response;
        }

        public static void SetClientDeactive(string serviceUrl, Client client)
        {
            string requestUrl = string.Format("{0}client/{1}/{2}", serviceUrl, client.ClientName, client.MacAddress);

            ResponseBuilder(requestUrl, "DELETE");


        }


        public static List<Client> GetAllClients(string serviceUrl)
        {
            string requestUrl = string.Format("{0}client", serviceUrl);

            DataContractSerializer ds = new DataContractSerializer(typeof(List<Client>));
            List<Client> ret = (List<Client>)ds.ReadObject(ResponseBuilder(requestUrl,"GET").GetResponseStream());

            return ret;

        }

        public static ClientSyncData AddNewTask(string imei, string sha, int sliceWidth, string serviceUrl) 
        {
            string requestUrl = string.Format("{0}task/{1}/{2}/{3}", serviceUrl,imei,sha,sliceWidth.ToString());

            DataContractSerializer ds = new DataContractSerializer(typeof(ClientSyncData));
            ClientSyncData ret = (ClientSyncData)ds.ReadObject(ResponseBuilder(requestUrl, "PUT").GetResponseStream());

            return ret;
        }

    }


}
